package com.allenkerr.utils;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

/**
 * @author 小柯
 * @date 2018/1/9
 */
public class StringUtil {

  /**
   * Returns true if url is valid.
   *
   * @param url
   * @return boolean
   */
  public static boolean isValidURL(String url) {
    try {
      new URL(url).toURI();
      return true;
    } catch (Exception e) {
      //ignore exception
      return false;
    }
  }

  /**
   * Concat some string into one string, if string is null,
   * convert it into "", instead of "null".
   *
   * @param format the specified format string
   * @param args   some string
   * @return String
   */
  public static String concat(String format, String... args) {
    return String.format(format, Arrays.stream(args).map(ObjectUtils::toString).toArray());
  }

  /**
   * Judge all string is not empty
   *
   * @param args some string
   * @return boolean
   */
  public static boolean areAllNotEmpty(String... args) {
    return ArrayUtils.isNotEmpty(args) && Arrays.stream(args).allMatch(StringUtils::isNotEmpty);
  }

  /**
   * Judge all string is empty
   *
   * @param args some string
   * @return boolean
   */
  public static boolean areAllEmpty(String... args) {
    return ArrayUtils.isEmpty(args) || Arrays.stream(args).allMatch(StringUtils::isEmpty);
  }

  /**
   * Judge any string is not empty
   *
   * @param args some string
   * @return boolean
   */
  public static boolean areAnyNotEmpty(String... args) {
    return ArrayUtils.isNotEmpty(args) && Arrays.stream(args).anyMatch(StringUtils::isNotEmpty);
  }

  /**
   * Judge any string is empty
   *
   * @param args some string
   * @return boolean
   */
  public static boolean areAnyEmpty(String... args) {
    return ArrayUtils.isEmpty(args) || Arrays.stream(args).anyMatch(StringUtils::isEmpty);
  }

  /**
   * Get first not empty string
   *
   * @param args
   * @return String
   */
  public static String getFirstNotEmptyString(String... args) {
    if (ArrayUtils.isEmpty(args)) {
      return null;
    }
    return Arrays.stream(args)
        .filter(StringUtils::isNotEmpty)
        .findFirst()
        .orElse(null);
  }

  /**
   * Compare the length of String with a length, whether the first argument is
   * greater than or equal to the second
   *
   * @param text
   * @param length
   * @return boolean
   */
  public static boolean longerThanOrEqualTo(String text, int length) {
    return StringUtils.length(text) >= length;
  }

  /**
   * Compare the length of String with a length, whether the first argument is
   * greater than the second
   *
   * @param text
   * @param length
   * @return boolean
   */
  public static boolean longerThan(String text, int length) {
    return StringUtils.length(text) > length;
  }

  /**
   * 是否包含中文
   *
   * @param text
   * @return boolean
   */
  public static boolean containsChinese(String text) {
    if (text == null) {
      return false;
    }
    for (char c : text.toCharArray()) {
      if (isChinese(c)) {
        return true;
      }
    }
    return false;
  }

  /**
   * 判断某个字符是否为汉字
   *
   * @param c 需要判断的字符
   * @return 是汉字返回true，否则返回false
   */
  public static boolean isChinese(char c) {
    String regex = "[\\u4e00-\\u9fa5]";
    return String.valueOf(c).matches(regex);
  }

  public static String toString(List<?> list) {
    if (list == null) {return null;}
    
    StringBuilder str = new StringBuilder();
    str.append("[");
    for (Object o : list) {
      str.append(String.valueOf(o)).append(",");
    }
    str.deleteCharAt(str.length() - 1);
    str.append("]");
    
    return str.toString();
  }
  
  private StringUtil() {
  }

}
